home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 2.iso / dist / fw_bzip2.idb / usr / freeware / info / bzip2.info-1.z / bzip2.info-1 (.txt)
GNU Info File  |  2000-06-09  |  46KB  |  862 lines

  1. This is Info file bzip2.info, produced by Makeinfo version 1.68 from
  2. the input file manual.texi.
  3. START-INFO-DIR-ENTRY
  4. * Bzip2: (bzip2).        A program and library for data compression.
  5. END-INFO-DIR-ENTRY
  6. File: bzip2.info,  Node: Top,  Next: Overview,  Prev: (dir),  Up: (dir)
  7.    This program, `bzip2', and associated library `libbzip2', are
  8. Copyright (C) 1996-1999 Julian R Seward.  All rights reserved.
  9.    Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are
  11.    *    Redistributions of source code must retain the above copyright
  12.       notice, this list of conditions and the following disclaimer.
  13.    *    The origin of this software must not be misrepresented; you must
  14.        not claim that you wrote the original software.  If you use this
  15.        software in a product, an acknowledgment in the product
  16.      documentation would be appreciated but is not required.
  17.    *    Altered source versions must be plainly marked as such, and must
  18.        not be misrepresented as being the original software.
  19.    *    The name of the author may not be used to endorse or promote
  20.      products derived from this software without specific prior written
  21.        permission.  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  25. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32.    Julian Seward, Cambridge, UK.
  33.    `jseward@acm.org'
  34.    `http://www.muraroa.demon.co.uk'
  35.    `bzip2'/`libbzip2' version 0.9.5 of 24 May 1999.
  36.    PATENTS: To the best of my knowledge, `bzip2' does not use any
  37. patented algorithms.  However, I do not have the resources available to
  38. carry out a full patent search.  Therefore I cannot give any guarantee
  39. of the above statement.
  40. * Menu:
  41. * Overview::                    An overview of bzip2
  42. * How to use bzip2::            Invoking bzip2
  43. * Programming with libbzip2::   Using the libbz library
  44. * Miscellanea::                 Random thoughts
  45.  -- The Detailed Node Listing --
  46. Programming with `libbzip2'
  47. * Top-level structure::
  48. * Error handling::
  49. * Low-level interface::
  50. * High-level interface::
  51. * Utility functions::
  52. * zlib compatibility functions::
  53. * Using the library in a stdio-free environment::
  54. * Making a Windows DLL::
  55. Miscellanea
  56. * Limitations of the compressed file format::
  57. * Portability issues::
  58. * Reporting bugs::
  59. * Did you get the right package?::
  60. * Testing::
  61. * Further reading::
  62. File: bzip2.info,  Node: Overview,  Next: How to use bzip2,  Prev: Top,  Up: Top
  63. Introduction
  64. ************
  65.    `bzip2'  compresses  files  using the Burrows-Wheeler block-sorting
  66. text compression algorithm,  and  Huffman  coding.  Compression  is
  67. generally  considerably  better than that achieved by more conventional
  68. LZ77/LZ78-based compressors, and  approaches  the performance of the
  69. PPM family of statistical compressors.
  70.    `bzip2' is built on top of `libbzip2', a flexible library for
  71. handling compressed data in the `bzip2' format.  This manual describes
  72. both how to use the program and how to work with the library interface.
  73. Most of the manual is devoted to this library, not the program, which
  74. is good news if your interest is only in the program.
  75.    Chapter 2 describes how to use `bzip2'; this is the only part you
  76. need to read if you just want to know how to operate the program.
  77. Chapter 3 describes the programming interfaces in detail, and Chapter 4
  78. records some miscellaneous notes which I thought ought to be recorded
  79. somewhere.
  80. * Menu:
  81. * How to use bzip2::
  82. * Programming with libbzip2::
  83. * Miscellanea::
  84. File: bzip2.info,  Node: How to use bzip2,  Next: Programming with libbzip2,  Prev: Overview,  Up: Top
  85. How to use `bzip2'
  86. ******************
  87.    This chapter contains a copy of the `bzip2' man page, and nothing
  88. else.
  89.  NAME
  90.         * `bzip2', `bunzip2' - a block-sorting file compressor, v0.9.5
  91.         * `bzcat' - decompresses files to stdout
  92.         * `bzip2recover' - recovers data from damaged bzip2 files
  93. SYNOPSIS
  94. ........
  95.         * `bzip2' [ -cdfkqstvzVL123456789 ] [ filenames ...  ]
  96.         * `bunzip2' [ -fkvsVL ] [ filenames ...  ]
  97.         * `bzcat' [ -s ] [ filenames ...  ]
  98.         * `bzip2recover' filename
  99. DESCRIPTION
  100. ...........
  101.      `bzip2' compresses files using the Burrows-Wheeler block sorting
  102. text compression algorithm, and Huffman coding.  Compression is
  103. generally considerably better than that achieved by more conventional
  104. LZ77/LZ78-based compressors, and approaches the performance of the PPM
  105. family of statistical compressors.
  106.      The command-line options are deliberately very similar to those of
  107. GNU `gzip', but they are not identical.
  108.      `bzip2' expects a list of file names to accompany the command-line
  109. flags.  Each file is replaced by a compressed version of itself, with
  110. the name `original_name.bz2'.  Each compressed file has the same
  111. modification date, permissions, and, when possible, ownership as the
  112. corresponding original, so that these properties can be correctly
  113. restored at decompression time.  File name handling is naive in the
  114. sense that there is no mechanism for preserving original file names,
  115. permissions, ownerships or dates in filesystems which lack these
  116. concepts, or have serious file name length restrictions, such as MS-DOS.
  117.      `bzip2' and `bunzip2' will by default not overwrite existing
  118. files.  If you want this to happen, specify the `-f' flag.
  119.      If no file names are specified, `bzip2' compresses from standard
  120. input to standard output.  In this case, `bzip2' will decline to write
  121. compressed output to a terminal, as this would be entirely
  122. incomprehensible and therefore pointless.
  123.      `bunzip2' (or `bzip2 -d') decompresses all specified files.  Files
  124. which were not created by `bzip2' will be detected and ignored, and a
  125. warning issued.  `bzip2' attempts to guess the filename for the
  126. decompressed file from that of the compressed file as follows:
  127.         * `filename.bz2 ' becomes `filename'
  128.         * `filename.bz  ' becomes `filename'
  129.         * `filename.tbz2' becomes `filename.tar'
  130.         * `filename.tbz ' becomes `filename.tar'
  131.         * `anyothername ' becomes `anyothername.out' If the file does
  132. not end in one of the recognised endings, `.bz2', `.bz', `.tbz2' or
  133. `.tbz', `bzip2' complains that it cannot guess the name of the original
  134. file, and uses the original name with `.out' appended.
  135.      As with compression, supplying no filenames causes decompression
  136. from standard input to standard output.
  137.      `bunzip2' will correctly decompress a file which is the
  138. concatenation of two or more compressed files.  The result is the
  139. concatenation of the corresponding uncompressed files.  Integrity
  140. testing (`-t') of concatenated compressed files is also supported.
  141.      You can also compress or decompress files to the standard output by
  142. giving the `-c' flag.  Multiple files may be compressed and
  143. decompressed like this.  The resulting outputs are fed sequentially to
  144. stdout.  Compression of multiple files in this manner generates a stream
  145. containing multiple compressed file representations.  Such a stream can
  146. be decompressed correctly only by `bzip2' version 0.9.0 or later.
  147. Earlier versions of `bzip2' will stop after decompressing the first
  148. file in the stream.
  149.      `bzcat' (or `bzip2 -dc') decompresses all specified files to the
  150. standard output.
  151.      `bzip2' will read arguments from the environment variables `BZIP2'
  152. and `BZIP', in that order, and will process them before any arguments
  153. read from the command line.  This gives a convenient way to supply
  154. default arguments.
  155.      Compression is always performed, even if the compressed file is
  156. slightly larger than the original.  Files of less than about one
  157. hundred bytes tend to get larger, since the compression mechanism has a
  158. constant overhead in the region of 50 bytes.  Random data (including
  159. the output of most file compressors) is coded at about 8.05 bits per
  160. byte, giving an expansion of around 0.5%.
  161.      As a self-check for your protection, `bzip2' uses 32-bit CRCs to
  162. make sure that the decompressed version of a file is identical to the
  163. original.  This guards against corruption of the compressed data, and
  164. against undetected bugs in `bzip2' (hopefully very unlikely).  The
  165. chances of data corruption going undetected is microscopic, about one
  166. chance in four billion for each file processed.  Be aware, though, that
  167. the check occurs upon decompression, so it can only tell you that
  168. something is wrong.  It can't help you recover the original uncompressed
  169. data.  You can use `bzip2recover' to try to recover data from damaged
  170. files.
  171.      Return values: 0 for a normal exit, 1 for environmental problems
  172. (file not found, invalid flags, I/O errors, &c), 2 to indicate a corrupt
  173. compressed file, 3 for an internal consistency error (eg, bug) which
  174. caused `bzip2' to panic.
  175. OPTIONS
  176. .......
  177.     `-c  --stdout'
  178.           Compress or decompress to standard output.
  179.     `-d  --decompress'
  180.           Force decompression.  `bzip2', `bunzip2' and `bzcat' are
  181.           really the same program, and the decision about what actions
  182.           to take is done on the basis of which name is used.  This
  183.           flag overrides that mechanism, and forces bzip2 to decompress.
  184.     `-z --compress'
  185.           The complement to `-d': forces compression, regardless of the
  186.           invokation name.
  187.     `-t --test'
  188.           Check integrity of the specified file(s), but don't
  189.           decompress them.  This really performs a trial decompression
  190.           and throws away the result.
  191.     `-f --force'
  192.           Force overwrite of output files.  Normally, `bzip2' will not
  193.           overwrite existing output files.  Also forces `bzip2' to
  194.           break hard links to files, which it otherwise wouldn't do.
  195.     `-k --keep'
  196.           Keep (don't delete) input files during compression or
  197.           decompression.
  198.     `-s --small'
  199.           Reduce memory usage, for compression, decompression and
  200.           testing.  Files are decompressed and tested using a modified
  201.           algorithm which only requires 2.5 bytes per block byte.  This
  202.           means any file can be decompressed in 2300k of memory, albeit
  203.           at about half the normal speed.
  204.           During compression, `-s' selects a block size of 200k, which
  205.           limits memory use to around the same figure, at the expense
  206.           of your compression ratio.  In short, if your machine is low
  207.           on memory (8 megabytes or less), use -s for everything.  See
  208.           MEMORY MANAGEMENT below.
  209.     `-q --quiet'
  210.           Suppress non-essential warning messages.  Messages pertaining
  211.           to I/O errors and other critical events will not be
  212.           suppressed.
  213.     `-v --verbose'
  214.           Verbose mode - show the compression ratio for each file
  215.           processed.  Further `-v''s increase the verbosity level,
  216.           spewing out lots of information which is primarily of
  217.           interest for diagnostic purposes.
  218.     `-L --license -V --version'
  219.           Display the software version, license terms and conditions.
  220.     `-1 to -9'
  221.           Set the block size to 100 k, 200 k ..  900 k when
  222.           compressing.  Has no effect when decompressing.  See MEMORY
  223.           MANAGEMENT below.
  224.     `--'
  225.           Treats all subsequent arguments as file names, even if they
  226.           start with a dash.  This is so you can handle files with
  227.           names beginning with a dash, for example: `bzip2 --
  228.           -myfilename'.
  229.     `--repetitive-fast'
  230.     `--repetitive-best'
  231.           These flags are redundant in versions 0.9.5 and above.  They
  232.           provided some coarse control over the behaviour of the
  233.           sorting algorithm in earlier versions, which was sometimes
  234.           useful.  0.9.5 and above have an improved algorithm which
  235.           renders these flags irrelevant.
  236. MEMORY MANAGEMENT
  237. .................
  238.      `bzip2' compresses large files in blocks.  The block size affects
  239. both the compression ratio achieved, and the amount of memory needed for
  240. compression and decompression.  The flags `-1' through `-9' specify the
  241. block size to be 100,000 bytes through 900,000 bytes (the default)
  242. respectively.  At decompression time, the block size used for
  243. compression is read from the header of the compressed file, and
  244. `bunzip2' then allocates itself just enough memory to decompress the
  245. file.  Since block sizes are stored in compressed files, it follows
  246. that the flags `-1' to `-9' are irrelevant to and so ignored during
  247. decompression.
  248.      Compression and decompression requirements, in bytes, can be
  249. estimated as:
  250.                Compression:   400k + ( 8 x block size )
  251.           
  252.                Decompression: 100k + ( 4 x block size ), or
  253.                               100k + ( 2.5 x block size )
  254.      Larger block sizes give rapidly diminishing marginal returns.
  255. Most of the compression comes from the first two or three hundred k of
  256. block size, a fact worth bearing in mind when using `bzip2' on small
  257. machines.  It is also important to appreciate that the decompression
  258. memory requirement is set at compression time by the choice of block
  259. size.
  260.      For files compressed with the default 900k block size, `bunzip2'
  261. will require about 3700 kbytes to decompress.  To support decompression
  262. of any file on a 4 megabyte machine, `bunzip2' has an option to
  263. decompress using approximately half this amount of memory, about 2300
  264. kbytes.  Decompression speed is also halved, so you should use this
  265. option only where necessary.  The relevant flag is `-s'.
  266.      In general, try and use the largest block size memory constraints
  267. allow, since that maximises the compression achieved.  Compression and
  268. decompression speed are virtually unaffected by block size.
  269.      Another significant point applies to files which fit in a single
  270. block - that means most files you'd encounter using a large block size.
  271. The amount of real memory touched is proportional to the size of the
  272. file, since the file is smaller than a block.  For example, compressing
  273. a file 20,000 bytes long with the flag `-9' will cause the compressor to
  274. allocate around 7600k of memory, but only touch 400k + 20000 * 8 = 560
  275. kbytes of it.  Similarly, the decompressor will allocate 3700k but only
  276. touch 100k + 20000 * 4 = 180 kbytes.
  277.      Here is a table which summarises the maximum memory usage for
  278. different block sizes.  Also recorded is the total compressed size for
  279. 14 files of the Calgary Text Compression Corpus totalling 3,141,622
  280. bytes.  This column gives some feel for how compression varies with
  281. block size.  These figures tend to understate the advantage of larger
  282. block sizes for larger files, since the Corpus is dominated by smaller
  283. files.
  284.                     Compress   Decompress   Decompress   Corpus
  285.              Flag     usage      usage       -s usage     Size
  286.           
  287.               -1      1200k       500k         350k      914704
  288.               -2      2000k       900k         600k      877703
  289.               -3      2800k      1300k         850k      860338
  290.               -4      3600k      1700k        1100k      846899
  291.               -5      4400k      2100k        1350k      845160
  292.               -6      5200k      2500k        1600k      838626
  293.               -7      6100k      2900k        1850k      834096
  294.               -8      6800k      3300k        2100k      828642
  295.               -9      7600k      3700k        2350k      828642
  296. RECOVERING DATA FROM DAMAGED FILES
  297. ..................................
  298.      `bzip2' compresses files in blocks, usually 900kbytes long.  Each
  299. block is handled independently.  If a media or transmission error causes
  300. a multi-block `.bz2' file to become damaged, it may be possible to
  301. recover data from the undamaged blocks in the file.
  302.      The compressed representation of each block is delimited by a
  303. 48-bit pattern, which makes it possible to find the block boundaries
  304. with reasonable certainty.  Each block also carries its own 32-bit CRC,
  305. so damaged blocks can be distinguished from undamaged ones.
  306.      `bzip2recover' is a simple program whose purpose is to search for
  307. blocks in `.bz2' files, and write each block out into its own `.bz2'
  308. file.  You can then use `bzip2 -t' to test the integrity of the
  309. resulting files, and decompress those which are undamaged.
  310.      `bzip2recover' takes a single argument, the name of the damaged
  311. file, and writes a number of files `rec0001file.bz2',
  312. `rec0002file.bz2', etc, containing the  extracted  blocks.         The
  313. output  filenames  are  designed  so  that the use of        wildcards
  314. in subsequent processing - for example, `bzip2 -dc  rec*file.bz2 >
  315. recovered_data' - lists the files in        the correct order.
  316.      `bzip2recover' should be of most use dealing with large `.bz2'
  317.   files,  as  these will contain many blocks.  It is clearly
  318. futile to use it on damaged single-block  files,  since  a
  319. damaged  block  cannot  be recovered.  If you wish to minimise any
  320. potential data loss through media  or  transmission errors, you might
  321. consider compressing with a smaller        block size.
  322. PERFORMANCE NOTES
  323. .................
  324.      The sorting phase of compression gathers together similar strings
  325. in the file.  Because of this, files containing very long runs of
  326. repeated symbols, like "aabaabaabaab ..."  (repeated several hundred
  327. times) may compress more slowly than normal.  Versions 0.9.5 and above
  328. fare much better than previous versions in this respect.  The ratio
  329. between worst-case and average-case compression time is in the region
  330. of 10:1.  For previous versions, this figure was more like 100:1.  You
  331. can use the `-vvvv' option to monitor progress in great detail, if you
  332. want.
  333.      Decompression speed is unaffected by these phenomena.
  334.      `bzip2' usually allocates several megabytes of memory to operate
  335. in, and then charges all over it in a fairly random fashion.  This means
  336. that performance, both for compressing and decompressing, is largely
  337. determined by the speed at which your machine can service cache misses.
  338. Because of this, small changes to the code to reduce the miss rate have
  339. been observed to give disproportionately large performance improvements.
  340. I imagine `bzip2' will perform best on machines with very large caches.
  341. CAVEATS
  342. .......
  343.      I/O error messages are not as helpful as they could be.  `bzip2'
  344. tries hard to detect I/O errors and exit cleanly, but the details of
  345. what the problem is sometimes seem rather misleading.
  346.      This manual page pertains to version 0.9.5 of `bzip2'.  Compressed
  347. data created by this version is entirely forwards and backwards
  348. compatible with the previous public releases, versions 0.1pl2 and 0.9.0,
  349. but with the following exception: 0.9.0 and above can correctly
  350. decompress multiple concatenated compressed files.  0.1pl2 cannot do
  351. this; it will stop after decompressing just the first file in the
  352. stream.
  353.      `bzip2recover' uses 32-bit integers to represent bit positions in
  354. compressed files, so it cannot handle compressed files more than 512
  355. megabytes long.  This could easily be fixed.
  356. AUTHOR
  357. ......
  358.      Julian Seward, `jseward@acm.org'.
  359.      The ideas embodied in `bzip2' are due to (at least) the following
  360. people: Michael Burrows and David Wheeler (for the block sorting
  361. transformation), David Wheeler (again, for the Huffman coder), Peter
  362. Fenwick (for the structured coding model in the original `bzip', and
  363. many refinements), and Alistair Moffat, Radford Neal and Ian Witten
  364. (for the arithmetic coder in the original `bzip').  I am much indebted
  365. for their help, support and advice.  See the manual in the source
  366. distribution for pointers to sources of documentation.  Christian von
  367. Roques encouraged me to look for faster sorting algorithms, so as to
  368. speed up compression.  Bela Lubkin encouraged me to improve the
  369. worst-case compression performance.  Many people sent patches, helped
  370. with portability problems, lent machines, gave advice and were generally
  371. helpful.
  372. File: bzip2.info,  Node: Programming with libbzip2,  Next: Miscellanea,  Prev: How to use bzip2,  Up: Top
  373. Programming with `libbzip2'
  374. ***************************
  375.    This chapter describes the programming interface to `libbzip2'.
  376.    For general background information, particularly about memory use
  377. and performance aspects, you'd be well advised to read Chapter 2 as
  378. well.
  379. * Menu:
  380. * Top-level structure::         Functions and data structures
  381. * Error handling::
  382. * Low-level interface::
  383. * High-level interface::
  384. * Utility functions::
  385. * zlib compatibility functions::
  386. * Using the library in a stdio-free environment::
  387. * Making a Windows DLL::
  388. File: bzip2.info,  Node: Top-level structure,  Next: Error handling,  Prev: Programming with libbzip2,  Up: Programming with libbzip2
  389. Top-level structure
  390. ===================
  391.    `libbzip2' is a flexible library for compressing and decompressing
  392. data in the `bzip2' data format.  Although packaged as a single entity,
  393. it helps to regard the library as three separate parts: the low level
  394. interface, and the high level interface, and some utility functions.
  395.    The structure of `libbzip2''s interfaces is similar to that of
  396. Jean-loup Gailly's and Mark Adler's excellent `zlib' library.
  397. Low-level summary
  398. -----------------
  399.    This interface provides services for compressing and decompressing
  400. data in memory.  There's no provision for dealing with files, streams
  401. or any other I/O mechanisms, just straight memory-to-memory work.  In
  402. fact, this part of the library can be compiled without inclusion of
  403. `stdio.h', which may be helpful for embedded applications.
  404.    The low-level part of the library has no global variables and is
  405. therefore thread-safe.
  406.    Six routines make up the low level interface: `bzCompressInit',
  407. `bzCompress', and
  408. `bzCompressEnd' for compression, and a corresponding trio
  409. `bzDecompressInit',
  410. `bzDecompress' and `bzDecompressEnd' for decompression.  The `*Init'
  411. functions allocate memory for compression/decompression and do other
  412. initialisations, whilst the `*End' functions close down operations and
  413. release memory.
  414.    The real work is done by `bzCompress' and `bzDecompress'.  These
  415. compress/decompress data from a user-supplied input buffer to a
  416. user-supplied output buffer.  These buffers can be any size; arbitrary
  417. quantities of data are handled by making repeated calls to these
  418. functions.  This is a flexible mechanism allowing a consumer-pull style
  419. of activity, or producer-push, or a mixture of both.
  420. High-level summary
  421. ------------------
  422.    This interface provides some handy wrappers around the low-level
  423. interface to facilitate reading and writing `bzip2' format files
  424. (`.bz2' files).  The routines provide hooks to facilitate reading files
  425. in which the `bzip2' data stream is embedded within some larger-scale
  426. file structure, or where there are multiple `bzip2' data streams
  427. concatenated end-to-end.
  428.    For reading files, `bzReadOpen', `bzRead', `bzReadClose' and
  429. `bzReadGetUnused' are supplied.  For writing files, `bzWriteOpen',
  430. `bzWrite' and `bzWriteFinish' are available.
  431.    As with the low-level library, no global variables are used so the
  432. library is per se thread-safe.  However, if I/O errors occur whilst
  433. reading or writing the underlying compressed files, you may have to
  434. consult `errno' to determine the cause of the error.  In that case,
  435. you'd need a C library which correctly supports `errno' in a
  436. multithreaded environment.
  437.    To make the library a little simpler and more portable, `bzReadOpen'
  438. and `bzWriteOpen' require you to pass them file handles (`FILE*'s)
  439. which have previously been opened for reading or writing respectively.
  440. That avoids portability problems associated with file operations and
  441. file attributes, whilst not being much of an imposition on the
  442. programmer.
  443. Utility functions summary
  444. -------------------------
  445.    For very simple needs, `bzBuffToBuffCompress' and
  446. `bzBuffToBuffDecompress' are provided.  These compress data in memory
  447. from one buffer to another buffer in a single function call.  You
  448. should assess whether these functions fulfill your memory-to-memory
  449. compression/decompression requirements before investing effort in
  450. understanding the more general but more complex low-level interface.
  451.    Yoshioka Tsuneo (`QWF00133@niftyserve.or.jp' /
  452. `tsuneo-y@is.aist-nara.ac.jp') has contributed some functions to give
  453. better `zlib' compatibility.  These functions are `bzopen', `bzread',
  454. `bzwrite', `bzflush', `bzclose', `bzerror' and `bzlibVersion'.  You may
  455. find these functions more convenient for simple file reading and
  456. writing, than those in the high-level interface.  These functions are
  457. not (yet) officially part of the library, and are minimally documented
  458. here.  If they break, you get to keep all the pieces.  I hope to
  459. document them properly when time permits.
  460.    Yoshioka also contributed modifications to allow the library to be
  461. built as a Windows DLL.
  462. File: bzip2.info,  Node: Error handling,  Next: Low-level interface,  Prev: Top-level structure,  Up: Programming with libbzip2
  463. Error handling
  464. ==============
  465.    The library is designed to recover cleanly in all situations,
  466. including the worst-case situation of decompressing random data.  I'm
  467. not 100% sure that it can always do this, so you might want to add a
  468. signal handler to catch segmentation violations during decompression if
  469. you are feeling especially paranoid.  I would be interested in hearing
  470. more about the robustness of the library to corrupted compressed data.
  471.    The file `bzlib.h' contains all definitions needed to use the
  472. library.  In particular, you should definitely not include
  473. `bzlib_private.h'.
  474.    In `bzlib.h', the various return values are defined.  The following
  475. list is not intended as an exhaustive description of the circumstances
  476. in which a given value may be returned - those descriptions are given
  477. later.  Rather, it is intended to convey the rough meaning of each
  478. return value.  The first five actions are normal and not intended to
  479. denote an error situation.
  480. `BZ_OK'
  481.      The requested action was completed successfully.
  482. `BZ_RUN_OK'
  483. `BZ_FLUSH_OK'
  484. `BZ_FINISH_OK'
  485.      In `bzCompress', the requested flush/finish/nothing-special action
  486.      was completed successfully.
  487. `BZ_STREAM_END'
  488.      Compression of data was completed, or the logical stream end was
  489.      detected during decompression.
  490.    The following return values indicate an error of some kind.
  491. `BZ_SEQUENCE_ERROR'
  492.      When using the library, it is important to call the functions in
  493.      the correct sequence and with data structures (buffers etc) in the
  494.      correct states.  `libbzip2' checks as much as it can to ensure
  495.      this is happening, and returns `BZ_SEQUENCE_ERROR' if not.  Code
  496.      which complies precisely with the function semantics, as detailed
  497.      below, should never receive this value; such an event denotes
  498.      buggy code which you should investigate.
  499. `BZ_PARAM_ERROR'
  500.      Returned when a parameter to a function call is out of range or
  501.      otherwise manifestly incorrect.  As with `BZ_SEQUENCE_ERROR', this
  502.      denotes a bug in the client code.  The distinction between
  503.      `BZ_PARAM_ERROR' and `BZ_SEQUENCE_ERROR' is a bit hazy, but still
  504.      worth making.
  505. `BZ_MEM_ERROR'
  506.      Returned when a request to allocate memory failed.  Note that the
  507.      quantity of memory needed to decompress a stream cannot be
  508.      determined until the stream's header has been read.  So
  509.      `bzDecompress' and `bzRead' may return `BZ_MEM_ERROR' even though
  510.      some of the compressed data has been read.  The same is not true
  511.      for compression; once `bzCompressInit' or `bzWriteOpen' have
  512.      successfully completed, `BZ_MEM_ERROR' cannot occur.
  513. `BZ_DATA_ERROR'
  514.      Returned when a data integrity error is detected during
  515.      decompression.  Most importantly, this means when stored and
  516.      computed CRCs for the data do not match.  This value is also
  517.      returned upon detection of any other anomaly in the compressed
  518.      data.
  519. `BZ_DATA_ERROR_MAGIC'
  520.      As a special case of `BZ_DATA_ERROR', it is sometimes useful to
  521.      know when the compressed stream does not start with the correct
  522.      magic bytes (`'B' 'Z' 'h'').
  523. `BZ_IO_ERROR'
  524.      Returned by `bzRead' and `bzRead' when there is an error reading
  525.      or writing in the compressed file, and by `bzReadOpen' and
  526.      `bzWriteOpen' for attempts to use a file for which the error
  527.      indicator (viz, `ferror(f)') is set.  On receipt of `BZ_IO_ERROR',
  528.      the caller should consult `errno' and/or `perror' to acquire
  529.      operating-system specific information about the problem.
  530. `BZ_UNEXPECTED_EOF'
  531.      Returned by `bzRead' when the compressed file finishes before the
  532.      logical end of stream is detected.
  533. `BZ_OUTBUFF_FULL'
  534.      Returned by `bzBuffToBuffCompress' and `bzBuffToBuffDecompress' to
  535.      indicate that the output data will not fit into the output buffer
  536.      provided.
  537. File: bzip2.info,  Node: Low-level interface,  Next: High-level interface,  Prev: Error handling,  Up: Programming with libbzip2
  538. Low-level interface
  539. ===================
  540. `bzCompressInit'
  541. ----------------
  542.      typedef
  543.         struct {
  544.            char *next_in;
  545.            unsigned int avail_in;
  546.            unsigned int total_in;
  547.      
  548.            char *next_out;
  549.            unsigned int avail_out;
  550.            unsigned int total_out;
  551.      
  552.            void *state;
  553.      
  554.            void *(*bzalloc)(void *,int,int);
  555.            void (*bzfree)(void *,void *);
  556.            void *opaque;
  557.         }
  558.         bz_stream;
  559.      
  560.      int bzCompressInit ( bz_stream *strm,
  561.                           int blockSize100k,
  562.                           int verbosity,
  563.                           int workFactor );
  564.    Prepares for compression.  The `bz_stream' structure holds all data
  565. pertaining to the compression activity.  A `bz_stream' structure should
  566. be allocated and initialised prior to the call.  The fields of
  567. `bz_stream' comprise the entirety of the user-visible data.  `state' is
  568. a pointer to the private data structures required for compression.
  569.    Custom memory allocators are supported, via fields `bzalloc',
  570. `bzfree', and `opaque'.  The value `opaque' is passed to as the first
  571. argument to all calls to `bzalloc' and `bzfree', but is otherwise
  572. ignored by the library.  The call `bzalloc ( opaque, n, m )' is
  573. expected to return a pointer `p' to `n * m' bytes of memory, and
  574. `bzfree ( opaque, p )' should free that memory.
  575.    If you don't want to use a custom memory allocator, set `bzalloc',
  576. `bzfree' and `opaque' to `NULL', and the library will then use the
  577. standard `malloc'/`free' routines.
  578.    Before calling `bzCompressInit', fields `bzalloc', `bzfree' and
  579. `opaque' should be filled appropriately, as just described.  Upon
  580. return, the internal state will have been allocated and initialised,
  581. and `total_in' and `total_out' will have been set to zero.  These last
  582. two fields are used by the library to inform the caller of the total
  583. amount of data passed into and out of the library, respectively.  You
  584. should not try to change them.
  585.    Parameter `blockSize100k' specifies the block size to be used for
  586. compression.  It should be a value between 1 and 9 inclusive, and the
  587. actual block size used is 100000 x this figure.  9 gives the best
  588. compression but takes most memory.
  589.    Parameter `verbosity' should be set to a number between 0 and 4
  590. inclusive.  0 is silent, and greater numbers give increasingly verbose
  591. monitoring/debugging output.  If the library has been compiled with
  592. `-DBZ_NO_STDIO', no such output will appear for any verbosity setting.
  593.    Parameter `workFactor' controls how the compression phase behaves
  594. when presented with worst case, highly repetitive, input data.  If
  595. compression runs into difficulties caused by repetitive data, the
  596. library switches from the standard sorting algorithm to a fallback
  597. algorithm.  The fallback is slower than the standard algorithm by
  598. perhaps a factor of three, but always behaves reasonably, no matter how
  599. bad the input.
  600.    Lower values of `workFactor' reduce the amount of effort the
  601. standard algorithm will expend before resorting to the fallback.  You
  602. should set this parameter carefully; too low, and many inputs will be
  603. handled by the fallback algorithm and so compress rather slowly, too
  604. high, and your average-to-worst case compression times can become very
  605. large.  The default value of 30 gives reasonable behaviour over a wide
  606. range of circumstances.
  607.    Allowable values range from 0 to 250 inclusive.  0 is a special case,
  608. equivalent to using the default value of 30.
  609.    Note that the compressed output generated is the same regardless of
  610. whether or not the fallback algorithm is used.
  611.    Be aware also that this parameter may disappear entirely in future
  612. versions of the library.  In principle it should be possible to devise a
  613. good way to automatically choose which algorithm to use.  Such a
  614. mechanism would render the parameter obsolete.
  615.    Possible return values:
  616.            `BZ_PARAM_ERROR'
  617.               if `strm' is `NULL'
  618.               or `blockSize' < 1 or `blockSize' > 9
  619.               or `verbosity' < 0 or `verbosity' > 4
  620.               or `workFactor' < 0 or `workFactor' > 250
  621.            `BZ_MEM_ERROR'
  622.               if not enough memory is available
  623.            `BZ_OK'
  624.               otherwise
  625.    Allowable next actions:
  626.            `bzCompress'
  627.               if `BZ_OK' is returned
  628.            no specific action needed in case of error
  629. `bzCompress'
  630. ------------
  631.         int bzCompress ( bz_stream *strm, int action );
  632.    Provides more input and/or output buffer space for the library.  The
  633. caller maintains input and output buffers, and calls `bzCompress' to
  634. transfer data between them.
  635.    Before each call to `bzCompress', `next_in' should point at the data
  636. to be compressed, and `avail_in' should indicate how many bytes the
  637. library may read.  `bzCompress' updates `next_in', `avail_in' and
  638. `total_in' to reflect the number of bytes it has read.
  639.    Similarly, `next_out' should point to a buffer in which the
  640. compressed data is to be placed, with `avail_out' indicating how much
  641. output space is available.  `bzCompress' updates `next_out',
  642. `avail_out' and `total_out' to reflect the number of bytes output.
  643.    You may provide and remove as little or as much data as you like on
  644. each call of `bzCompress'.  In the limit, it is acceptable to supply and
  645. remove data one byte at a time, although this would be terribly
  646. inefficient.  You should always ensure that at least one byte of output
  647. space is available at each call.
  648.    A second purpose of `bzCompress' is to request a change of mode of
  649. the compressed stream.
  650.    Conceptually, a compressed stream can be in one of four states: IDLE,
  651. RUNNING, FLUSHING and FINISHING.  Before initialisation
  652. (`bzCompressInit') and after termination (`bzCompressEnd'), a stream is
  653. regarded as IDLE.
  654.    Upon initialisation (`bzCompressInit'), the stream is placed in the
  655. RUNNING state.  Subsequent calls to `bzCompress' should pass `BZ_RUN'
  656. as the requested action; other actions are illegal and will result in
  657. `BZ_SEQUENCE_ERROR'.
  658.    At some point, the calling program will have provided all the input
  659. data it wants to.  It will then want to finish up - in effect, asking
  660. the library to process any data it might have buffered internally.  In
  661. this state, `bzCompress' will no longer attempt to read data from
  662. `next_in', but it will want to write data to `next_out'.  Because the
  663. output buffer supplied by the user can be arbitrarily small, the
  664. finishing-up operation cannot necessarily be done with a single call of
  665. `bzCompress'.
  666.    Instead, the calling program passes `BZ_FINISH' as an action to
  667. `bzCompress'.  This changes the stream's state to FINISHING.  Any
  668. remaining input (ie, `next_in[0 .. avail_in-1]') is compressed and
  669. transferred to the output buffer.  To do this, `bzCompress' must be
  670. called repeatedly until all the output has been consumed.  At that
  671. point, `bzCompress' returns `BZ_STREAM_END', and the stream's state is
  672. set back to IDLE.  `bzCompressEnd' should then be called.
  673.    Just to make sure the calling program does not cheat, the library
  674. makes a note of `avail_in' at the time of the first call to
  675. `bzCompress' which has `BZ_FINISH' as an action (ie, at the time the
  676. program has announced its intention to not supply any more input).  By
  677. comparing this value with that of `avail_in' over subsequent calls to
  678. `bzCompress', the library can detect any attempts to slip in more data
  679. to compress.  Any calls for which this is detected will return
  680. `BZ_SEQUENCE_ERROR'.  This indicates a programming mistake which should
  681. be corrected.
  682.    Instead of asking to finish, the calling program may ask
  683. `bzCompress' to take all the remaining input, compress it and terminate
  684. the current (Burrows-Wheeler) compression block.  This could be useful
  685. for error control purposes.  The mechanism is analogous to that for
  686. finishing: call `bzCompress' with an action of `BZ_FLUSH', remove
  687. output data, and persist with the `BZ_FLUSH' action until the value
  688. `BZ_RUN' is returned.  As with finishing, `bzCompress' detects any
  689. attempt to provide more input data once the flush has begun.
  690.    Once the flush is complete, the stream returns to the normal RUNNING
  691. state.
  692.    This all sounds pretty complex, but isn't really.  Here's a table
  693. which shows which actions are allowable in each state, what action will
  694. be taken, what the next state is, and what the non-error return values
  695. are.  Note that you can't explicitly ask what state the stream is in,
  696. but nor do you need to - it can be inferred from the values returned by
  697. `bzCompress'.
  698.      IDLE/`any'
  699.            Illegal.  IDLE state only exists after `bzCompressEnd' or
  700.            before `bzCompressInit'.
  701.            Return value = `BZ_SEQUENCE_ERROR'
  702.      
  703.      RUNNING/`BZ_RUN'
  704.            Compress from `next_in' to `next_out' as much as possible.
  705.            Next state = RUNNING
  706.            Return value = `BZ_RUN_OK'
  707.      
  708.      RUNNING/`BZ_FLUSH'
  709.            Remember current value of `next_in'.  Compress from `next_in'
  710.            to `next_out' as much as possible, but do not accept any more input.
  711.            Next state = FLUSHING
  712.            Return value = `BZ_FLUSH_OK'
  713.      
  714.      RUNNING/`BZ_FINISH'
  715.            Remember current value of `next_in'.  Compress from `next_in'
  716.            to `next_out' as much as possible, but do not accept any more input.
  717.            Next state = FINISHING
  718.            Return value = `BZ_FINISH_OK'
  719.      
  720.      FLUSHING/`BZ_FLUSH'
  721.            Compress from `next_in' to `next_out' as much as possible,
  722.            but do not accept any more input.
  723.            If all the existing input has been used up and all compressed
  724.            output has been removed
  725.               Next state = RUNNING; Return value = `BZ_RUN_OK'
  726.            else
  727.               Next state = FLUSHING; Return value = `BZ_FLUSH_OK'
  728.      
  729.      FLUSHING/other
  730.            Illegal.
  731.            Return value = `BZ_SEQUENCE_ERROR'
  732.      
  733.      FINISHING/`BZ_FINISH'
  734.            Compress from `next_in' to `next_out' as much as possible,
  735.            but to not accept any more input.
  736.            If all the existing input has been used up and all compressed
  737.            output has been removed
  738.               Next state = IDLE; Return value = `BZ_STREAM_END'
  739.            else
  740.               Next state = FINISHING; Return value = `BZ_FINISHING'
  741.      
  742.      FINISHING/other
  743.            Illegal.
  744.            Return value = `BZ_SEQUENCE_ERROR'
  745.    That still looks complicated?  Well, fair enough.  The usual sequence
  746. of calls for compressing a load of data is:
  747.    * Get started with `bzCompressInit'.
  748.    * Shovel data in and shlurp out its compressed form using zero or
  749.      more calls of `bzCompress' with action = `BZ_RUN'.
  750.    * Finish up.  Repeatedly call `bzCompress' with action = `BZ_FINISH',
  751.      copying out the compressed output, until `BZ_STREAM_END' is
  752.      returned.
  753.    * Close up and go home.  Call `bzCompressEnd'.  If the data you want
  754. to compress fits into your input buffer all at once, you can skip the
  755. calls of `bzCompress ( ..., BZ_RUN )' and just do the `bzCompress (
  756. ..., BZ_FINISH )' calls.
  757.    All required memory is allocated by `bzCompressInit'.  The
  758. compression library can accept any data at all (obviously).  So you
  759. shouldn't get any error return values from the `bzCompress' calls.  If
  760. you do, they will be `BZ_SEQUENCE_ERROR', and indicate a bug in your
  761. programming.
  762.    Trivial other possible return values:
  763.            `BZ_PARAM_ERROR'
  764.               if `strm' is `NULL', or `strm->s' is `NULL'
  765. `bzCompressEnd'
  766. ---------------
  767.      int bzCompressEnd ( bz_stream *strm );
  768.    Releases all memory associated with a compression stream.
  769.    Possible return values:
  770.         `BZ_PARAM_ERROR'    if `strm' is `NULL' or `strm->s' is `NULL'
  771.         `BZ_OK'    otherwise
  772. `bzDecompressInit'
  773. ------------------
  774.      int bzDecompressInit ( bz_stream *strm, int verbosity, int small );
  775.    Prepares for decompression.  As with `bzCompressInit', a `bz_stream'
  776. record should be allocated and initialised before the call.  Fields
  777. `bzalloc', `bzfree' and `opaque' should be set if a custom memory
  778. allocator is required, or made `NULL' for the normal `malloc'/`free'
  779. routines.  Upon return, the internal state will have been initialised,
  780. and `total_in' and `total_out' will be zero.
  781.    For the meaning of parameter `verbosity', see `bzCompressInit'.
  782.    If `small' is nonzero, the library will use an alternative
  783. decompression algorithm which uses less memory but at the cost of
  784. decompressing more slowly (roughly speaking, half the speed, but the
  785. maximum memory requirement drops to around 2300k).  See Chapter 2 for
  786. more information on memory management.
  787.    Note that the amount of memory needed to decompress a stream cannot
  788. be determined until the stream's header has been read, so even if
  789. `bzDecompressInit' succeeds, a subsequent `bzDecompress' could fail
  790. with `BZ_MEM_ERROR'.
  791.    Possible return values:
  792.            `BZ_PARAM_ERROR'
  793.               if `(small != 0 && small != 1)'
  794.               or `(verbosity < 0 || verbosity > 4)'
  795.            `BZ_MEM_ERROR'
  796.               if insufficient memory is available
  797.    Allowable next actions:
  798.            `bzDecompress'
  799.               if `BZ_OK' was returned
  800.            no specific action required in case of error
  801. `bzDecompress'
  802. --------------
  803.      int bzDecompress ( bz_stream *strm );
  804.    Provides more input and/out output buffer space for the library.  The
  805. caller maintains input and output buffers, and uses `bzDecompress' to
  806. transfer data between them.
  807.    Before each call to `bzDecompress', `next_in' should point at the
  808. compressed data, and `avail_in' should indicate how many bytes the
  809. library may read.  `bzDecompress' updates `next_in', `avail_in' and
  810. `total_in' to reflect the number of bytes it has read.
  811.    Similarly, `next_out' should point to a buffer in which the
  812. uncompressed output is to be placed, with `avail_out' indicating how
  813. much output space is available.  `bzCompress' updates `next_out',
  814. `avail_out' and `total_out' to reflect the number of bytes output.
  815.    You may provide and remove as little or as much data as you like on
  816. each call of `bzDecompress'.  In the limit, it is acceptable to supply
  817. and remove data one byte at a time, although this would be terribly
  818. inefficient.  You should always ensure that at least one byte of output
  819. space is available at each call.
  820.    Use of `bzDecompress' is simpler than `bzCompress'.
  821.    You should provide input and remove output as described above, and
  822. repeatedly call `bzDecompress' until `BZ_STREAM_END' is returned.
  823. Appearance of `BZ_STREAM_END' denotes that `bzDecompress' has detected
  824. the logical end of the compressed stream.  `bzDecompress' will not
  825. produce `BZ_STREAM_END' until all output data has been placed into the
  826. output buffer, so once `BZ_STREAM_END' appears, you are guaranteed to
  827. have available all the decompressed output, and `bzDecompressEnd' can
  828. safely be called.
  829.    If case of an error return value, you should call `bzDecompressEnd'
  830. to clean up and release memory.
  831.    Possible return values:
  832.            `BZ_PARAM_ERROR'
  833.               if `strm' is `NULL' or `strm->s' is `NULL'
  834.               or `strm->avail_out < 1'
  835.            `BZ_DATA_ERROR'
  836.               if a data integrity error is detected in the compressed stream
  837.            `BZ_DATA_ERROR_MAGIC'
  838.               if the compressed stream doesn't begin with the right magic bytes
  839.            `BZ_MEM_ERROR'
  840.               if there wasn't enough memory available
  841.            `BZ_STREAM_END'
  842.               if the logical end of the data stream was detected and all
  843.               output in has been consumed, eg `s->avail_out > 0'
  844.            `BZ_OK'
  845.               otherwise
  846.    Allowable next actions:
  847.            `bzDecompress'
  848.               if `BZ_OK' was returned
  849.            `bzDecompressEnd'
  850.               otherwise
  851. `bzDecompressEnd'
  852. -----------------
  853.      int bzDecompressEnd ( bz_stream *strm );
  854.    Releases all memory associated with a decompression stream.
  855.    Possible return values:
  856.            `BZ_PARAM_ERROR'
  857.               if `strm' is `NULL' or `strm->s' is `NULL'
  858.            `BZ_OK'
  859.               otherwise
  860.    Allowable next actions:
  861.            None.
  862.